﻿#include "MachineBase.h"
#include <glfw3.h>

QuizMachine::QuizMachine()
{
	// Initialize basic objct member
	m_MachineState = MACHINESTATE_NONE;

	// Initialize class member
	m_QuestionPool = new QuestionPool();
	m_QuestionProvider = new QuestionProvider();
	m_SceneRender = NULL;

	m_CorrectCount = 0;
	m_WrongCount = 0;
	m_LastQuestionCorrect = false;
}

QuizMachine::~QuizMachine()
{
	delete m_QuestionPool;
	delete m_QuestionProvider;
}

// Initialize
void QuizMachine::Init()
{
	QuestionFactory factory;
	factory.LoadQuestionPool(m_QuestionPool);

	if(m_QuestionProvider->BindQuestionPool(m_QuestionPool))
	{
		// Change state
		m_MachineState = QUIZMACHINESTATE_WAITINGANSWER;
	}
	else
	{
		printf("Bind question pool failed!\n");
	}
}

void QuizMachine::Run()
{
	switch(m_MachineState)
	{
	case QUIZMACHINESTATE_WAITINGANSWER:
		{
			glViewport(640,0,640,480);
			PrintQuestion();
			break;
		}
	case QUIZMACHINESTATE_SHOWRESULT:
		{
			glViewport(640,0,640,480);
			DisplayResult();
			break;
		}
	default:
		break;
	}
}

void QuizMachine::InputKeyHandler(int key)
{
	switch(m_MachineState)
	{
		case MACHINESTATE_START:
			{
				break;
			}
		case QUIZMACHINESTATE_WAITINGANSWER:
			{
				// Option A
				if(key == GLFW_KEY_1)
				{
					CheckAnswer(OptionA);

					m_MachineState = QUIZMACHINESTATE_SHOWRESULT;
				}

				// Option B
				else if(key == GLFW_KEY_2)
				{
					CheckAnswer(OptionB);

					m_MachineState = QUIZMACHINESTATE_SHOWRESULT;
				}
				else
				{
					printf("Input 1 or 2 \n");
				}
			}
			break;
		case QUIZMACHINESTATE_SHOWRESULT:
			{
				// Press any key and go to next question
				m_QuestionProvider->Next();
				PrintQuestion();

				m_MachineState = QUIZMACHINESTATE_WAITINGANSWER;

			}
			break;
		default:
			printf("Quiz machine won't do anything now!\n");
			break;
	}
}

void QuizMachine::PrintQuestion()
{
	if(m_SceneRender == NULL)
	{
		// Put some message here
	}
	else
	{
		wstring header = m_QuestionProvider->GetCurrentIndexString();
		wstring title =  m_QuestionProvider->GetCurrentQuestionTitle();
		wstring optionA = L"A. " + m_QuestionProvider->GetCurrentOptionA();
		wstring optionB = L"B. " + m_QuestionProvider->GetCurrentOptionB();

		if(header.length() > 0 && header.length() > 0)
			m_SceneRender->SetQuestionData(header,title,optionA,optionB);
	}


}

void QuizMachine::DisplayResult()
{
	if(m_SceneRender == NULL)
	{
		
	}
	else
	{
		wchar_t buffer1[100];
		wchar_t buffer2[100];
		wchar_t buffer3[100];
		wchar_t buffer4[100];

		wstring str1;
		wstring str2;
		wstring str3;
		wstring str4;

		if(m_LastQuestionCorrect)
			swprintf(buffer1,100,L"恭喜，答对了!");
		else
			swprintf(buffer1,100,L"真不走运，刚才那题答错了。");

		swprintf(buffer2,100,L"一共回答了: %d 题",m_CorrectCount + m_WrongCount);
		swprintf(buffer3,100,L"正确: %d",m_CorrectCount);
		swprintf(buffer4,100,L"错误: %d",m_WrongCount);

		str1 = buffer1;
		str2 = buffer2;
		str3 = buffer3;
		str4 = buffer4;

		m_SceneRender->SetStatData(str1,str2,str3,str4);
	}
}

void QuizMachine::SetRender(SceneRender* sceneRender)
{
	m_SceneRender = sceneRender;
}

// Go to next question.
// Always make this action when one question is checked,
// and should never call this function directly outside class.
void QuizMachine::NextQuestion()
{
	m_QuestionProvider->Next();
}

void QuizMachine::CheckAnswer(QuestionOption option)
{
	switch(option)
	{
		case OptionA:
			{
				if(m_QuestionProvider->GetCurrentQuestionAnswer().compare(m_QuestionProvider->GetCurrentOptionA()) == 0)
				{
					//printf("Great! You are correct! Press any key to continue ! \n");
					m_CorrectCount ++;
					m_LastQuestionCorrect = true;
				}
				else
				{
					//printf("Wrong answer\n");
					m_WrongCount ++;
					m_LastQuestionCorrect = false;
				}
			}
			break;
		case OptionB:
			{
				if(m_QuestionProvider->GetCurrentQuestionAnswer().compare(m_QuestionProvider->GetCurrentOptionB()) == 0)
				{
					m_CorrectCount ++;
					m_LastQuestionCorrect = true;
				}
				else
				{
					m_WrongCount++;
					m_LastQuestionCorrect = false;
				}
			}
			break;
		default:
			printf("I don't know what it is. \n");
			break;

	}
}

bool QuizMachine::IsLastQuestion()
{
	return m_QuestionProvider->IsLastQuestion();
}

// Close Quiz machine
void QuizMachine::Exit()
{
	printf("Quit the Quiz Machine ! \n");
	printf("Good bye ! \n");

	// Clear state
	m_MachineState = MACHINESTATE_END;
}